package com.pth.interceptor;


import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.pth.action.LoginAction;

@SuppressWarnings("serial")
public class LoginInterceptor extends AbstractInterceptor {

	@SuppressWarnings("unchecked")
	@Override
	public String intercept(ActionInvocation arg0) throws Exception {
		
	if(LoginAction.class==arg0.getAction().getClass()){
			
			return arg0.invoke();
			
		}
	
		Map map=arg0.getInvocationContext().getSession();
		
		if(null==map.get("userinfo")){
		
			HttpServletRequest req=ServletActionContext.getRequest();
			String fromURL = req.getHeader("Referer");  
			HttpSession session=req.getSession();
			session.setAttribute("from", fromURL);
			
			return Action.LOGIN;
		}
		
		
		
		return arg0.invoke();
	
	}

}
